<?php
/**************************************
 *
 * @Repositories 数据层~主要与数据库打交道
 *
 **************************************/
namespace app\common\repositories;

use app\common\model\IndexModel;

use think\Model;

use think\Db;

class SpecialRepositories
{
    protected $IndexModel;

    public function __construct()
    {
        $this->IndexModel = new IndexModel();
    }

    /*
     *  专题列表
     */
    public function getList($requestData)
    {
        $user_id = null;
        $map = [];
        if (isset($requestData['id'])) {
            $map['id'] = array('lt',$requestData['id']);
        }
        if (isset($requestData['user_id'])) {
            $user_id = $requestData['user_id'];
        }
        $item = Db::table('special')
                ->field('id, img, follow_num, title')
                ->where($map)
                ->order('id desc')
//                ->limit(8)
                ->select();
//        foreach ($item as &$v) {
//            $v['img'] = basename($v['img']);
//        }
        $data = [];
        for ($i=0; $i<count($item);$i++) {
            $data[$i]['id'] = $item[$i]['id'];
            $data[$i]['user_id'] = $user_id;
        }
        $clickRepositories = new ClickRepositories();
        $followNum = $clickRepositories->followNum($data);
        for ($i=0; $i<count($item);$i++) {
            $item[$i]['follow_num'] = $followNum[$i]['follow_num'];
            $item[$i]['whether'] = $followNum[$i]['whether'];
        }

        if ($item) {
            return $item;
        } else {
            return false;
        }
    }

    /*
     *  关注的专题列表
     */
    public function followList($requestData)
    {
        $map = [];
        $user_id = $requestData['user_id'];
        //获取用户关注过的专题
        $clickRepositories = new ClickRepositories();
        $followNum = $clickRepositories->getFollowSp($user_id);
        if (!$followNum) {
            return false;
        }
        if ($followNum) {
//            if (isset($requestData['id'])) {
//            $map['a.id'] = array('lt',$requestData['id']);
//        }
//        $map['a.table'] = 'special';
//        $map['a.user_id'] = $user_id;
//        $item = Db::table('click_follow')
//            ->alias('a')
//            ->join('special b', 'a.table_id = b.id', 'LEFT')
//            ->field('a.id f_id, b.img')
//            ->limit(6)
//            ->where($map)
//            ->select();
//
//    } else {
            $arr = array_values($followNum);
            $map['id'] = array('in', $arr);
            $item = Db::table('special')
                ->field('id, img, title')
                ->where($map)
                ->select();
        }
//        $item = array_reverse($item);
//        foreach ($item as &$v) {
//            $v['img'] = basename($v['img']);
//        }
        if ($item) {
            return $item;
        } else {
            return false;
        }
    }

    /*
     *  专题详情
     */
    public function spInfo($requestData)
    {
        $map['id'] = $requestData['id'];
        $item = Db::table('special')->where($map)->find();
        //文章数
        $cout = Db::table('article')->where(['sp_id' => $requestData['id'] , 'status' => 1])->count();
        //关注头像
        $clickRepositories = new ClickRepositories();
        $followImg = $clickRepositories->getFollowImg($requestData['id']);
        $data[] = $requestData;
        $followNum = $clickRepositories->followNum($data);
        if ($followNum) {
            $item['follow_num'] = $followNum[0]['follow_num'];
            $item['whether'] = $followNum[0]['whether'];
        }
        $item['cout'] = $cout;
        $item['user_img'] = $followImg;
        if ($item) {
            return $item;
        } else {
            return false;
        }
    }

    /*
     *  专题相关文章
     */
    public function spRelevant($requestData)
    {
        $user_id = null;
        if (isset($requestData['user_id'])) {
            $user_id = $requestData['user_id'];
        }
        $map['sp_id'] = $requestData['id'];
        $map['status'] = 1;
        $item = Db::table('article')->where($map)->order('id desc')->select();
        if (!$item) {
            return false;
        }
        //用户点赞数
        $clickRepositories = new ClickRepositories();
        for ($i=0; $i<count($item);$i++) {
            $data[$i]['id'] = $item[$i]['id'];
            $data[$i]['user_id'] = $user_id;
        }
        $clickNum = $clickRepositories->clickNum($data);
        //重组数组
        if ($clickNum) {
            for ($i=0; $i<count($item);$i++) {
                $item[$i]['click'] = $clickNum[$i]['click'];
                $item[$i]['whether'] = $clickNum[$i]['whether'];
            }
        }
        //最新点赞的三个头像
        $newImg = $clickRepositories->getClickImg($data);
        for ($i=0; $i<count($item);$i++) {
            $item[$i]['newImg'] = $newImg[$i]['click'];
        }
        if ($item) {
            return $item;
        } else {
            return false;
        }

    }
}